home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap07 / Connect / Connect.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  3.4 KB  |  111 lines

  1. /*--------------------------------------------------
  2.    CONNECT.C -- Connect-the-Dots Mouse Demo Program
  3.                 (c) Charles Petzold, 1998
  4.   --------------------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. #define MAXPOINTS 1000
  9.  
  10. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  11.  
  12. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  13.                     PSTR szCmdLine, int iCmdShow)
  14. {
  15.      static TCHAR szAppName[] = TEXT ("Connect") ;
  16.      HWND         hwnd ;
  17.      MSG          msg ;
  18.      WNDCLASS     wndclass ;
  19.      
  20.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  21.      wndclass.lpfnWndProc   = WndProc ;
  22.      wndclass.cbClsExtra    = 0 ;
  23.      wndclass.cbWndExtra    = 0 ;
  24.      wndclass.hInstance     = hInstance ;
  25.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  26.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  27.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  28.      wndclass.lpszMenuName  = NULL ;
  29.      wndclass.lpszClassName = szAppName ;
  30.      
  31.      if (!RegisterClass (&wndclass))
  32.      {
  33.           MessageBox (NULL, TEXT ("Program requires Windows NT!"), 
  34.                       szAppName, MB_ICONERROR) ;
  35.           return 0 ;
  36.      }
  37.      
  38.      hwnd = CreateWindow (szAppName, TEXT ("Connect-the-Points Mouse Demo"),
  39.                           WS_OVERLAPPEDWINDOW,
  40.                           CW_USEDEFAULT, CW_USEDEFAULT,
  41.                           CW_USEDEFAULT, CW_USEDEFAULT,
  42.                           NULL, NULL, hInstance, NULL) ;
  43.      
  44.      ShowWindow (hwnd, iCmdShow) ;
  45.      UpdateWindow (hwnd) ;
  46.      
  47.      while (GetMessage (&msg, NULL, 0, 0))
  48.      {
  49.           TranslateMessage (&msg) ;
  50.           DispatchMessage (&msg) ;
  51.      }
  52.      return msg.wParam ;
  53. }
  54.  
  55. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  56. {
  57.      static POINT pt[MAXPOINTS] ;
  58.      static int   iCount ;
  59.      HDC          hdc ;
  60.      int          i, j ;
  61.      PAINTSTRUCT  ps ;
  62.  
  63.      switch (message)
  64.      {
  65.      case WM_LBUTTONDOWN:
  66.           iCount = 0 ;
  67.           InvalidateRect (hwnd, NULL, TRUE) ;
  68.           return 0 ;
  69.           
  70.      case WM_MOUSEMOVE:
  71.           if (wParam & MK_LBUTTON && iCount < 1000)
  72.           {
  73.                pt[iCount  ].x = LOWORD (lParam) ;
  74.                pt[iCount++].y = HIWORD (lParam) ;
  75.                
  76.                hdc = GetDC (hwnd) ;
  77.                SetPixel (hdc, LOWORD (lParam), HIWORD (lParam), 0) ;
  78.                ReleaseDC (hwnd, hdc) ;
  79.           }
  80.           return 0 ;
  81.           
  82.      case WM_LBUTTONUP:
  83.           InvalidateRect (hwnd, NULL, FALSE) ;
  84.           return 0 ;
  85.           
  86.      case WM_PAINT:
  87.           hdc = BeginPaint (hwnd, &ps) ;
  88.           
  89.           SetCursor (LoadCursor (NULL, IDC_WAIT)) ;
  90.           ShowCursor (TRUE) ;
  91.           
  92.           for (i = 0 ; i < iCount - 1 ; i++)
  93.                for (j = i + 1 ; j < iCount ; j++)
  94.                {
  95.                     MoveToEx (hdc, pt[i].x, pt[i].y, NULL) ;
  96.                     LineTo   (hdc, pt[j].x, pt[j].y) ;
  97.                }
  98.                
  99.           ShowCursor (FALSE) ;
  100.           SetCursor (LoadCursor (NULL, IDC_ARROW)) ;
  101.                
  102.           EndPaint (hwnd, &ps) ;
  103.           return 0 ;
  104.                
  105.      case WM_DESTROY:
  106.           PostQuitMessage (0) ;
  107.           return 0 ;
  108.      }
  109.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  110. }
  111.